#include "poly2d.h"

Point::Point() : x_(0.0), y_(0.0) {}
Point::Point(double x, double y) : x_(x), y_(y) {}
double Point::x()
{
	return x_;
}
double Point::y()
{
	return y_;
}

LinearIneq::LinearIneq(Point &point1, Point &point2, Point inner_point)
{
	double x1 = point1.x();
	double y1 = point1.y();
	double x2 = point2.x();
	double y2 = point2.y();
	double delta_s = std::pow(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2), 0.5);
	if (x1 == x2 && y1 == y2)
	{
		const std::string error_message = "LinearIneq constructor failed， two point is the same.";
		throw std::runtime_error(error_message);
	}
	coff_y_ = (x2 - x1) / delta_s;
	coff_x_ = (y1 - y2) / delta_s;
	coff_const_ = (y1 * (x1 - x2) + x1 * (y2 - y1)) / delta_s;
	set_greater_than_zero(inner_point);
}
double LinearIneq::coff_x()
{
	return coff_x_;
}
double LinearIneq::coff_y()
{
	return coff_y_;
}
double LinearIneq::coff_const()
{
	return coff_const_;
}
bool LinearIneq::is_greater_than_zero()
{
	return is_greater_than_zero_;
}
void LinearIneq::set_greater_than_zero(Point inner_point)
{
	is_greater_than_zero_ = (coff_x_ * inner_point.x() + coff_y_ * inner_point.y() + coff_const_ > 0);
}

Poly2d::Poly2d(std::vector<Point> &vetex_point, const Point inner_point)
{
	if (vetex_point.size() <= 3)
	{
		const std::string error_message = "LinearIneq constructor failed,due to point num is less than 3.";
		throw std::runtime_error(error_message);
	}
	for (int i = 0; i < vetex_point.size() - 1; i++)
	{
		LinearIneq temp_ineq(vetex_point[i], vetex_point[i + 1]);
		temp_ineq.set_greater_than_zero(inner_point);
		ineq_vec_.push_back(temp_ineq);
	}
	LinearIneq temp_ineq(vetex_point[vetex_point.size() - 1], vetex_point[0]);
	temp_ineq.set_greater_than_zero(inner_point);
	ineq_vec_.push_back(temp_ineq);
}
